package cn.xiuxius.askbox.controller.console;


import cn.xiuxius.askbox.common.constant.DataState;
import cn.xiuxius.askbox.pojo.dto.query.AnswerQueryDTO;
import cn.xiuxius.askbox.pojo.dto.update.AnswerUpdateDTO;
import cn.xiuxius.askbox.pojo.dto.update.AskUpdateStateDTO;
import cn.xiuxius.askbox.pojo.result.PageResult;
import cn.xiuxius.askbox.pojo.result.Result;
import cn.xiuxius.askbox.pojo.vo.AnswerVO;
import cn.xiuxius.askbox.service.IAnswerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaoxiu
 * @since 2024-07-20
 */
@Api(tags = "后台-回复接口")
@RestController("consoleAnswerController")
@RequestMapping("/console/answers")
public class AnswerController {
    @Autowired
    private IAnswerService answerService;

    @ApiOperation("分页查询所有回复")
    @GetMapping
    public Result getAnswersByPage(@Validated AnswerQueryDTO dto) {
        //设置为有效数据
        dto.setState(DataState.ALIVE);
        PageResult result = answerService.getAnswersByPage(dto);
        return Result.success(result);
    }

    @ApiOperation("根据回答uuid查询回答")
    @GetMapping("/{uuid}")
    public Result getAnswerByAnswerUuid(@PathVariable String uuid) {
        AnswerQueryDTO dto = new AnswerQueryDTO();
        //设置为有效数据
        dto.setState(DataState.ALIVE);
        dto.setUuid(uuid);
        AnswerVO result = answerService.getAnswerByAnswerUuid(dto);
        return Result.success(result);
    }

    @ApiOperation("根据回答uuid更新回答")
    @PutMapping
    public Result updateAnswerByAnswerUuid(@RequestBody AnswerUpdateDTO dto) {
        answerService.updateAnswerByAnswerUuid(dto);
        return Result.success();
    }

    @ApiOperation("分页查询回收站的回答")
    @GetMapping("/deleted")
    public Result getDeletedAnswersByPage(@Validated AnswerQueryDTO dto) {
        dto.setState(DataState.DELETED);
        PageResult pageResult = answerService.getAnswersByPage(dto);
        return Result.success(pageResult);
    }

    @ApiOperation("分页查询回收站的回答")
    @GetMapping("/deleted/{uuid}")
    public Result getDeletedAnswersByAnswerUuid(@PathVariable String uuid) {
        AnswerQueryDTO dto = new AnswerQueryDTO();
        dto.setUuid(uuid);
        dto.setState(DataState.DELETED);
        AnswerVO vo = answerService.getAnswerByAnswerUuid(dto);
        return Result.success(vo);
    }

    @ApiOperation("根据回答uuid更新回答状态")
    @PutMapping("/state")
    public Result updateStateAnswerByAskUuid(@Validated @RequestBody AskUpdateStateDTO dto) {
        answerService.updateStateAnswerByAskUuid(dto);
        return Result.success();
    }

}
